package com.wenxy.common;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;


public class OpenFileIntent
{
  private static Intent getHtmlFileIntent(String param)
  {
    Uri uri = Uri.parse(param).buildUpon()
      .encodedAuthority("com.android.htmlfileprovider")
      .scheme("content").encodedPath(param).build();
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.setDataAndType(uri, "text/html");
    return intent;
  }

  private static Intent getImageFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "image/*");
    return intent;
  }

  private static Intent getPdfFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "application/pdf");
    return intent;
  }

  private static Intent getTextFileIntent(String param, boolean paramBoolean)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    if (paramBoolean) {
      Uri uri1 = Uri.parse(param);
      intent.setDataAndType(uri1, "text/plain");
    } else {
      Uri uri2 = Uri.fromFile(new File(param));
      intent.setDataAndType(uri2, "text/plain");
    }
    return intent;
  }

  private static Intent getAudioFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addFlags(67108864);
    intent.putExtra("oneshot", 0);
    intent.putExtra("configchange", 0);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "audio/*");
    return intent;
  }

  private static Intent getVideoFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addFlags(67108864);
    intent.putExtra("oneshot", 0);
    intent.putExtra("configchange", 0);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "video/*");
    return intent;
  }

  private static Intent getChmFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "application/x-chm");
    return intent;
  }

  private static Intent getWordFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    Log.d("HTTP_CLIENT", "File path1:" + param);
    Uri uri = Uri.fromFile(new File(param));
    Log.d("HTTP_CLIENT", "File path2:" + uri.getPath());
    intent.setDataAndType(uri, "application/msword");
    return intent;
  }

  private static Intent getExcelFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "application/vnd.ms-excel");
    return intent;
  }

  private static Intent getPptFileIntent(String param)
  {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    return intent;
  }

  private static Intent getNoneFileIntent(String param) {
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(268435456);
    Uri uri = Uri.fromFile(new File(param));
    intent.setDataAndType(uri, "application/*");
    return intent;
  }

  public static Intent getIntent(Context context, String param) {
    Intent intent = null;
    if ((param.endsWith(".doc")) || (param.endsWith(".docx")))
      intent = getWordFileIntent(param);
    else if ((param.endsWith(".html")) || (param.endsWith(".htm")))
      intent = getHtmlFileIntent(param);
    else if ((param.endsWith(".png")) || (param.endsWith(".gif")) || (param.endsWith(".jpg")))
      intent = getImageFileIntent(param);
    else if (param.endsWith(".pdf"))
      intent = getPdfFileIntent(param);
    else if (param.endsWith(".txt"))
      intent = getTextFileIntent(param, false);
    else if ((param.endsWith(".mp3")) || (param.endsWith(".m4a")) || (param.endsWith(".mid")))
      intent = getAudioFileIntent(param);
    else if ((param.endsWith(".3gp")) || (param.endsWith(".mp4")))
      intent = getVideoFileIntent(param);
    else if (param.endsWith(".chm"))
      intent = getChmFileIntent(param);
    else if ((param.endsWith(".xls")) || (param.endsWith(".xlsx")))
      intent = getExcelFileIntent(param);
    else if (param.endsWith(".ppt"))
      intent = getPptFileIntent(param);
    else {
      intent = getNoneFileIntent(param);
    }
    if (isIntentAvailable(context, intent)) {
      return intent;
    }
    return getNoneFileIntent(param);
  }

  private static boolean isIntentAvailable(Context context, Intent intent) {
    PackageManager packageManager = context.getPackageManager();
    List list = packageManager.queryIntentActivities(intent, 1);
    return list.size() > 0;
  }
}
